using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIItemUse : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIItemUse),
            layer = EUILayer.Pop,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIItemUse
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnReduce { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnAdd { protected set; get; }
            public IQIGame.Onigao.Framework.ExSlider slider { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public IQIGame.Onigao.Framework.ExText haveText { protected set; get; }
            public IQIGame.Onigao.Framework.ExText useText { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv0);
                this.btnCancel = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv1);
                this.btnConfirm = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnReduce", out var __tbv2);
                this.btnReduce = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnAdd", out var __tbv3);
                this.btnAdd = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExSlider>("slider", out var __tbv4);
                this.slider = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv5);
                this.textTitle = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("haveText", out var __tbv6);
                this.haveText = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("useText", out var __tbv7);
                this.useText = __tbv7;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region fields & properties
        private NumerCappedRange limitNum = NumerCappedRange.NewFromPool();
        UD_ItemUse dataItemUse => _data as UD_ItemUse;

        protected UIB_UIItemUse ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIItemUse();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnCancel.onClick.AddListener(this.Close);
            this.ui.btnConfirm.onClick.AddListener(this.OnClickUse);
            this.ui.btnAdd.onClick.AddListener(this.OnClickAdd);
            this.ui.btnReduce.onClick.AddListener(this.OnClickReduce);
            this.ui.slider.onValueChanged.AddListener(this.OnBarValueChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.limitNum?.Put2Pool();
            this.limitNum = null;
        }

        protected override void OnShow(UIBaseData data)
        {
            UD_ItemUse uData = (UD_ItemUse)data;

            if (uData == null || uData.cntOnceChange <= 0)
                return;
            var cfgData = uData.cfgData;
            if (cfgData == null)
                return;
            int nMax = ItemModule.Instance.GetCanUseMaxNum(uData.itemCid, uData.itemSid);
            this.limitNum.Sync(1, nMax);
            this.ShowSlider();
            this.ui.textTitle.text = uData.strTitle;
            this.ui.haveText.text = APIUIItemUse.GetStrLimitUse(this.limitNum.maxIntValue);
            this.ShowCanUse();
        }

        void ShowSlider()
        {
            this.ui.slider.SetNumberCapped(this.limitNum);
        }

        void ShowCanUse()
        {
            this.ui.useText.text = APIUIItemUse.GetStrUsing(this.limitNum.currentIntValue);
        }

        void OnClickUse()
        {
            if (this.limitNum.current == 0 || this.dataItemUse == null)
            {
                // 提示
                return;
            }
            this.SendUseAsync().Forget();
        }

        async UniTaskVoid SendUseAsync()
        {
            var result = await ItemModule.CS_useItem(dataItemUse.itemSid, dataItemUse.itemCid, limitNum.currentIntValue);
            if (result.IsSuccess)
            {
                ItemModule.Instance.UpItemCdInfo(result.result.itemCdInfo);
                this.Close();
                NoticeModule.ShowUIItemGet(result.result.addItems);
            }
        }

        void OnBarValueChanged(float val)
        {
            int current = (int)val;
            if (this.limitNum.SetCurrentReturnIsChanged(current))
                this.ShowCanUse();
        }

        void ReCalcSlilder(bool isAdd)
        {
            int changeValue = this.dataItemUse.cntOnceChange * (isAdd ? 1 : -1);
            if (this.limitNum.AddCurrentReturnIsChanged(changeValue))
            {
                this.ShowCanUse();
                this.ShowSlider();
            }
        }

        void OnClickAdd()
        {
            this.ReCalcSlilder(true);
        }

        void OnClickReduce()
        {
            this.ReCalcSlilder(false);
        }
    }
}
